package com.ygqh.baby.service.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.stereotype.Service;
import  org.springframework.util.Assert;

import com.ygqh.baby.ao.BalanceType;
import com.ygqh.baby.ao.CouponType;
import  com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.OperationType;
import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.UseType;
import  com.ygqh.baby.mapper.YgCouponUseDetailMapper;
import com.ygqh.baby.model.CouponUseDetailMode;
import com.ygqh.baby.po.YgCoupon;
import  com.ygqh.baby.po.YgCouponUseDetail;
import  com.ygqh.baby.po.YgCouponUseDetailExample;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.po.YgUserBalanceDetail;
import com.ygqh.baby.service.YgCouponDetailService;
import com.ygqh.baby.service.YgCouponService;
import  com.ygqh.baby.service.YgCouponUseDetailService;
import com.ygqh.baby.service.YgUserBalanceDetailService;
import com.ygqh.baby.service.YgUserBalanceService;

@Service
public class YgCouponUseDetailServiceImpl implements YgCouponUseDetailService{
	
	@Autowired
	private YgCouponUseDetailMapper ygCouponUseDetailMapper;
    @Autowired
    private YgUserBalanceService userBalanceService;
	@Autowired
	private YgUserBalanceDetailService userBalanceDetailService;
	@Autowired
	private YgCouponDetailService couponDetailService;
    @Autowired
    private YgCouponService ygCouponService;
	
	@Override
	public List<YgCouponUseDetail> find() {
		YgCouponUseDetailExample example = new YgCouponUseDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygCouponUseDetailMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgCouponUseDetailExample example = new YgCouponUseDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygCouponUseDetailMapper.countByExample(example);
	}

	@Override
	public YgCouponUseDetail findById(Long id) {
		return ygCouponUseDetailMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgCouponUseDetail ygCouponUseDetail) {
		return ygCouponUseDetailMapper.insertSelective(ygCouponUseDetail);
	}

	@Override
	public int update(YgCouponUseDetail ygCouponUseDetail) {
		return ygCouponUseDetailMapper.updateByPrimaryKeySelective(ygCouponUseDetail);
	}

	@Override
	public ResultSet<YgCouponUseDetail> search(QueryInfo queryInfo,String q) {
		List<YgCouponUseDetail>  list =  ygCouponUseDetailMapper.selectSuper(queryInfo, q);
		int count = ygCouponUseDetailMapper.countSuper(q);
		ResultSet<YgCouponUseDetail> resultSet = new ResultSet<YgCouponUseDetail>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgCouponUseDetailExample example = new YgCouponUseDetailExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgCouponUseDetail record = new YgCouponUseDetail();
		record.setStatus(DataStatus.Delete);

		return ygCouponUseDetailMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgCouponUseDetail ygCouponUseDetail) {
		int r = 0;
		if(ygCouponUseDetail.getId() != null) {
			r = this.update(ygCouponUseDetail);
		}else {
			r = this.save(ygCouponUseDetail);
		}
		return r;
	}

	@Override
	public YgCouponUseDetail findCouponUseDetailByOrderId(Long userId, String orderId) {
		
		return ygCouponUseDetailMapper.selectCouponUseDetailByOrderId(userId, orderId);
	}

	@Override
	public List<YgCouponUseDetail> findCouponUseDetailByShareUserId(Long userId, Long shareUserId) {
		
		return ygCouponUseDetailMapper.selectCouponUseDetailByShareUserId(userId, shareUserId);
	}

	@Override
	public String saveUseCoupon(int selectId,Object[][] prizeArr, YgUser user, String orderId,YgUser shareUser) {
		String msg="";
		try {
			YgCouponUseDetail ygCouponUseDetail=new YgCouponUseDetail();
			ygCouponUseDetail.setUserId(user.getId());
			Long relationId = null;
			if(selectId==0 ||selectId==1 || selectId==2){
				relationId=useCouponRecharge(user.getId(),new BigDecimal((int)prizeArr[selectId][1]));
				ygCouponUseDetail.setUseType(UseType.Fund);
			}else{
				YgCoupon coupon = ygCouponService.findById(Long.parseLong(prizeArr[selectId][1]+""));
				if (coupon.getCouponType().equals(CouponType.Postpone)) {
					relationId=couponDetailService.usePostponeCoupon(coupon, user);
				}
				ygCouponUseDetail.setUseType(UseType.Cash);
			}
			ygCouponUseDetail.setShareUserId(shareUser.getId());
			ygCouponUseDetail.setRelationId(relationId);
			ygCouponUseDetail.setCreateTime(new Date());
			if(orderId !=null){
				ygCouponUseDetail.setOrderId(Long.parseLong(orderId));
			}
			ygCouponUseDetail.setRemark(prizeArr[selectId][2]+"");
			this.save(ygCouponUseDetail);
			msg="000"+selectId;
		} catch (NumberFormatException e) {
			msg="0100";
		}
		
		return msg;
	}
	
	public Long useCouponRecharge(Long userId,BigDecimal price){
		
		userBalanceService.backAcountPrice(userId, price);
		
		YgUserBalanceDetail ygUserBalanceDetail = new YgUserBalanceDetail();
		ygUserBalanceDetail.setUserId(userId);
		ygUserBalanceDetail.setBalancePrice(price);
		ygUserBalanceDetail.setPriceType(BalanceType.Balance);
		ygUserBalanceDetail.setOperation(OperationType.Activity.getTitle());
		ygUserBalanceDetail.setOperationType(OperationType.Activity);
		ygUserBalanceDetail.setCreateTime(new Date());
		ygUserBalanceDetail.setRemark("领取活动礼包");
		userBalanceDetailService.save(ygUserBalanceDetail);
		return ygUserBalanceDetail.getId();
	}

	@Override
	public List<CouponUseDetailMode> findByOrderId(Long orderId) {
		if (orderId == null) {
			return new ArrayList<>();
		}
		return ygCouponUseDetailMapper.selectByOrderId(orderId);
	}
	
}

